package com.myhome.service.impl;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.myhome.domain.AccountDetail;
import com.myhome.domain.Coin;
import com.myhome.domain.Config;
import com.myhome.mapper.AccountDetailMapper;
import com.myhome.service.CoinService;
import com.myhome.service.ConfigService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.core.context.SecurityContextHolder;
import org.springframework.stereotype.Service;
import javax.annotation.Resource;
import java.math.BigDecimal;
import java.util.Date;
import java.util.List;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.myhome.domain.Account;
import com.myhome.mapper.AccountMapper;
import com.myhome.service.AccountService;
import org.springframework.util.StringUtils;

/**
*@Author QYiXin 
* @Date 2021 10 20 09 37
**/
@Service
public class AccountServiceImpl extends ServiceImpl<AccountMapper, Account> implements AccountService{
    @Autowired
    AccountDetailMapper accountDetailMapper;
    @Autowired
    CoinService coinService;
    @Autowired
    ConfigService configService;

    @Override
    public Boolean cashRechargeUpdate(BigDecimal num, Long auditId, Long userId, Byte status, int i, String remark, Long coinId, BigDecimal fee, Long orderId) {
        Account account = this.getOne(new LambdaQueryWrapper<Account>().eq(userId != null, Account::getUserId, userId).eq(coinId != null, Account::getCoinId, coinId));
        if (account == null) {
            throw new IllegalArgumentException("不存在该账户");
        }
        AccountDetail accountDetail = new AccountDetail();
        accountDetail.setAccountId(account.getId());
        accountDetail.setRefAccountId(account.getId());
        accountDetail.setAmount(BigDecimal.valueOf(1));
        accountDetail.setCreated(new Date());
        accountDetail.setDirection((byte) 1);
        accountDetail.setBusinessType("充值");
        accountDetail.setFee(fee);
        accountDetail.setRemark(remark);
        accountDetail.setCoinId(coinId);
        accountDetail.setOrderId(orderId);
        accountDetail.setUserId(userId);
        int insert = accountDetailMapper.insert(accountDetail);
        if (insert <= 0) {
            throw new IllegalArgumentException("插入失败");
        }
        account.setLastUpdateTime(new Date());
        account.setBalanceAmount(account.getBalanceAmount().add(num));
        return this.updateById(account);
    }

    @Override
    public Account getAccountByCoinName(String coinName) {
        Coin coin = coinService.getOne(new LambdaQueryWrapper<Coin>().eq(!StringUtils.isEmpty(coinName), Coin::getName, coinName));
        if (coin == null) {
            throw new IllegalArgumentException("该币种不存在");
        }
        Long id = coin.getId();
        Long userId = Long.valueOf(SecurityContextHolder.getContext().getAuthentication().getPrincipal().toString());
        Account account = this.getOne(new LambdaQueryWrapper<Account>().eq(Account::getCoinId, id).eq(Account::getUserId, userId));
        if (account == null) {
            throw new IllegalArgumentException("该资产不存在");
        }
        Config sellRateConfig = configService.getConfigByCode("USDT2CNY");
        account.setSellRate(new BigDecimal(sellRateConfig.getValue())); // 出售的费率

        Config setBuyRateConfig = configService.getConfigByCode("CNY2USDT");
        account.setBuyRate(new BigDecimal(setBuyRateConfig.getValue())); // 买进来的费率
        return account;
    }

    @Override
    public Account getUserAccountInfoByUserId(Long userId,Long coinId) {
        return this.getOne(new LambdaQueryWrapper<Account>().eq(userId != null, Account::getUserId, userId).eq(coinId != null, Account::getCoinId, coinId));
    }
}
